package com.bridgeintelligent.tag.webserver.dim.mgmt.service.dim;

import com.bridgeintelligent.tag.constants.ExceptionCodes;
import com.bridgeintelligent.tag.mapper.DimMapper;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.BaseCode;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.DimException;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.DimRep;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.model.DimNumModel;
import com.bridgeintelligent.tag.webserver.dim.mgmt.service.catalog.model.DimListModel;
import com.bridgeintelligent.tag.webserver.dim.mgmt.service.dim.vo.DimVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by JackCheung on 2019/9/9 10:04 PM.
 */
@Api(tags = "基础标签管理&标签查看")
@RestController
@RequestMapping("api/dimMgmt")
public class DimController {

    private IDimService iDimService;
    private DimMapper dimMapper;

    public DimController(IDimService iDimService) {
        this.iDimService = iDimService;
    }

    @ApiOperation(value = "查看单个标签详情", notes = "基础标签管理&标签查看")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "dimId", value = "标签ID", required = true)})
    @GetMapping("/detail")
    public DimRep detail(String dimId) {
        return iDimService.detail(dimId);
    }

    /*@ApiOperation(value = "新建标签", notes = "基础标签管理&标签查看")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "dimJson", value = "json格式字符串", required = true)})
    @PostMapping("/add")
    public DimRep add(String dimJson) {
        return iDimService.add(dimJson);
    }*/

    @ApiOperation(value = "新建标签", notes = "基础标签管理&标签查看")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", name = "dimVO", value = "DimVO信息", required = true)})
    @PostMapping("/add")
    public DimRep add(@RequestBody DimVO dimVO) {
        return iDimService.addEntity(dimVO);
    }


    @ApiOperation(value = "修改标签状态", notes = "基础标签管理&标签查看")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "state", value = "状态标识 标签状态:01表示启用中,02表示停用,03表示停用,04待审批 ", required = true), @ApiImplicitParam(paramType = "query", name = "dimId", value = "标签ID", required = true)})
    @PostMapping("/changeState")
    public DimRep start(String state, String dimId) {
        return iDimService.start(state, dimId);
    }

    @ApiOperation(value = "标签停用", notes = "基础标签管理&标签查看")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "dimId", value = "标签ID", required = true)})
    @PostMapping("/stop")
    public DimRep stop(String dimId) {
        return iDimService.stop(dimId);
    }

    /*@ApiOperation(value = "修改标签", notes = "基础标签管理&标签查看")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "dimJson", value = "json格式字符串", required = true)})
    @PostMapping("/update")
    public DimRep update(String dimJson) {
        return iDimService.update(dimJson);
    }*/

    @ApiOperation(value = "修改标签", notes = "基础标签管理&标签查看")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "dimVO", value = "DimVO信息", required = true)})
    @PostMapping("/update")
    public DimRep update(@RequestBody DimVO dimVO) {
        return iDimService.updateEntity(dimVO);
    }

    @ApiOperation(value = "获取标签列表", notes = "基础标签管理&标签查看")
    @GetMapping("list")
    public DimRep list(@ModelAttribute DimListModel dimListModel) {
        dimListModel.setPageNumber(dimListModel.getPageNumber() == null ? 1 : dimListModel.getPageNumber());
        dimListModel.setPageSize(dimListModel.getPageSize() == null ? 10 : dimListModel.getPageSize());
        return iDimService.list(dimListModel.getPageNumber(), dimListModel.getPageSize(), dimListModel.getTheme(), dimListModel.getDimName(), dimListModel.getDepartment(), dimListModel.getState(), dimListModel.getCreateTimeStart(), dimListModel.getCreateTimeEnd(), dimListModel.getUptTimeStart(), dimListModel.getUptTimeEnd(), dimListModel.getCatalogId(), dimListModel.getBusinessDepartment());
    }

    @ApiOperation(value = "获取标签列表", notes = "基础标签管理&标签查看")
    @GetMapping("dimList")
    public DimRep dimList(@ModelAttribute DimListModel dimListModel) {
        dimListModel.setPageNumber(dimListModel.getPageNumber() == null ? 1 : dimListModel.getPageNumber());
        dimListModel.setPageSize(dimListModel.getPageSize() == null ? 10 : dimListModel.getPageSize());
        return iDimService.dimList(dimListModel.getPageNumber(), dimListModel.getPageSize(), dimListModel.getTheme(), dimListModel.getDimName(), dimListModel.getDepartment(), dimListModel.getState(), dimListModel.getCreateTimeStart(), dimListModel.getCreateTimeEnd(), dimListModel.getUptTimeStart(), dimListModel.getUptTimeEnd(), dimListModel.getCatalogId(), dimListModel.getBusinessDepartment());
    }

    @ApiOperation(value = "删除基础标签", notes = "基础标签管理&标签查看")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "dimId", value = "标签ID", required = true)})
    @PostMapping("/del")
    public DimRep del(String dimId) {
        try {
            DimVO dimVo = (DimVO) iDimService.detail(dimId)
                                             .getData();
            if ("01".equals(dimVo.getDim()
                                 .getState())) {
                throw new DimException(ExceptionCodes.DIMFREE_90004);
            }
            return iDimService.del(dimId);
        } catch (DimException e) {
            return new DimRep(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "获取标签所在目录id列表", notes = "基础标签管理&标签查看")
    @GetMapping("inCatalogList")
    public DimRep inCatalogList(String dimName) {
        return iDimService.inCatalogList(dimName);
    }

    @ApiOperation(value = "获取数据时间", notes = "标签查询")
    @GetMapping("/esTime")
    public DimRep date() {
        return iDimService.esTimeData();
    }

    @ApiOperation(value = "获取输出内容时间", notes = "标签查询")
    @GetMapping("/esTimeOutput")
    public DimRep esTimeOutput() {
        return iDimService.esTimeOutput();
    }

    @ApiOperation(value = "获取输出内容时间", notes = "标签查询")
    @GetMapping("/esTimeLast")
    public DimRep esTimeLast() {
        return iDimService.esTimeLast();
    }


    @ApiOperation(value = "新版-获取ES数据时间", notes = "标签查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "indexId", value = "索引ID")
    })
    @GetMapping("/esDate")
    public DimRep esDate(String indexId) {
        return iDimService.newEsTime(indexId);
    }

    @ApiOperation(value = "新版-获取ES时间与固定标识的映射关系", notes = "标签查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "indexId", value = "索引ID")
    })
    @GetMapping("/mapDate")
    public DimRep mapDate(String indexId) {
        return iDimService.mapEsTime(indexId);
    }

    @ApiOperation(value = "获取启用的数值型标签", notes = "获取启用的数值型标签")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "dimId", value = "标签id"),
            @ApiImplicitParam(paramType = "query", name = "dimName", value = "标签名称"),
            @ApiImplicitParam(paramType = "query", name = "indexId", value = "索引ID")
    })
    @GetMapping("/numberDimList")
    public DimRep numberDimList(String dimId, String dimName,String indexId) {
        try {
            List<DimNumModel> list =  iDimService.numberDimList(dimName,dimId,indexId);
            return new DimRep(list, BaseCode.SUCCESS);
        }catch (Exception e){
            return new DimRep(BaseCode.FAIL,"查询异常"+e.getMessage());
        }


    }
}
